package com.ifp.opengate.boot.utils;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.ifp.cache.handle.CacheHandler;
import com.ifp.core.context.ClogicContext;
import com.ifp.core.log.Trace;
import com.ifp.core.util.SpringContextsUtil;
import com.ifp.opengate.boot.constants.Constants;
import com.ifp.opengate.boot.task.ApiDeployTask;

@Component("whiteListCacheUtil")
public class WhiteListCaheUtil {

    /*****
     * 直接从redis查询信息
     */
    public static boolean getCacheCheckWhite11(CacheHandler cache, String appId, String interceptor, String env) {
        Trace.logInfo(Trace.MODULE_CACHE, "===>>>白名单缓存校验<<<===");
        Trace.logInfo(Trace.MODULE_CACHE, "转换前：" + env);
        Trace.logInfo(Trace.MODULE_CACHE, "转换后：" + env);
        try {
            Map<String, String> whiteMap =
                (Map<String, String>)cache.get(Constants.OPEN_GATE_CACHE_NAME, "", Constants.CACHE_VALUE_TYPE_MAP);
            String mapItemKey = "whiteList_app_" + appId + "_" + interceptor;
            Trace.logInfo(Trace.MODULE_CACHE, "whitelist key:" + mapItemKey);
            String state = whiteMap.get(mapItemKey);
            Trace.logInfo(Trace.MODULE_CACHE, "whitelist state:" + state);
            if (state.equals("1")) {
                return true;
            }
        } catch (Exception e) {
            Trace.logInfo(Trace.MODULE_CACHE, "===>>>白名单缓存空<<<===");
        }
        return false;
    }

    /**
     * 从缓存bean里接获取信息
     */
    public static boolean getCacheCheckWhiteFromBean(Map<Object, Object> whiteMap, String appId, String interceptor,
        ClogicContext clogicContext) {
        Trace.logInfo(Trace.MODULE_CACHE, "===>>>白名单缓存校验<<<===");
        if ("15230274563295297134".equals(appId)) {
            return false;
        }

        String apiInfoKey = Constants.API_INFO_PREFIX + clogicContext.getTransCode();
        ApiDeployTask apiDeployTask = (ApiDeployTask)SpringContextsUtil.getBean("apiDeployTask");
        Map<Object, Object> apiInfo = apiDeployTask.getApiInfoMap().get(Constants.OPEN_GATE_CACHE_NAME + "_"
            + apiInfoKey + Constants.BOTTOM_LINE + clogicContext.getValue(Constants.VERSION));

        // TODO: 对外的服务不可以加白名单。服务是对外的即服务下的API也是对外的。是否对外开放0-否 1-是
        // opengate_api_info_/foodservice/OPC321560108080100000100_1.0.0
        // opengate_api_info_/foodservice/OPC321560108080100000100_1.0.0
        if (apiInfo == null || !"0".equals(apiInfo.get("ifOut"))) {
            /*
             * 不是对内的即对外的服务
             * 对外开放的服务都不可以走白名单
             * ps:王总提的，测试组也有这项测试案例
             */
            return false;
        }

        try {
            String mapItemKey = "whiteList_app_" + appId + "_" + interceptor;
            Trace.logInfo(Trace.MODULE_CACHE, "whitelist key:" + mapItemKey);
            String state = (String)whiteMap.get(mapItemKey);
            Trace.logInfo(Trace.MODULE_CACHE, "whitelist state:" + state);
            if (state.equals("1")) {
                return true;
            }
        } catch (Exception e) {
            Trace.logInfo(Trace.MODULE_CACHE, "===>>>白名单缓存空<<<===");
        }
        return false;
    }

}
